//
//  ScanQRController.swift
//  wechatManager
//
//  Created by tangchunhui on 15/4/15.
//  Copyright (c) 2015年 tangchunhui. All rights reserved.
//

import Foundation

class ScanQrController :UIViewController{
    
    
    var pati_id:String!
    var sick_no:String!
    var pati_name:String!
    var pati_sex:String!
    var age:String!
    var md5str:String!
    
    @IBOutlet var sick_noLabel:UILabel!
    @IBOutlet var nameLabel:UILabel!
    @IBOutlet var sexLabel:UILabel!
    @IBOutlet var ageLabel:UILabel!
    @IBOutlet var timeLabel:UILabel!
    @IBOutlet var errLabel:UILabel!
    @IBOutlet var qrImage:UIImageView!
    
    var sec = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        self.sexLabel.text = self.pati_sex
        self.sick_noLabel.text = self.sick_no
        self.nameLabel.text = self.pati_name
        self.ageLabel.text = self.age
        var modal = ModalQRInfo(md5str:self.md5str,sick_no: self.sick_noLabel!.text, pati_id: self.pati_id, pati_name: self.nameLabel!.text, pati_sex: self.sexLabel!.text)
        DataModal.instance().executeModal(modal, withhud: true, completion:{(data:Dictionary!)->()  in
            var data1:Dictionary<String,String> = data as! Dictionary<String,String>
            if (data1["result"]=="0"){
                var url :String = data1["qrurl"]!
                println(url)
                dispatch_sync(dispatch_get_main_queue(), {
                   var image = UIImage(data: NSData(contentsOfURL: NSURL(string: url)!)!)
                   self.qrImage.image = image
                    var timer:NSTimer  = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "handleTimer:", userInfo: nil, repeats: true)
                    self.sec=120
 
                })
                
            } else {
                self.view.backgroundColor = UIColor.grayColor();
                self.errLabel.text = data1["message"] 
                
            }
        },failure:nil)
    }
    func handleTimer(timer:NSTimer){
        self.sec = self.sec - 1
        if (self.sec == -1){
            self.navigationController?.popViewControllerAnimated(true)
        } else {
            self.timeLabel.text = String(self.sec)
        }
        
    }
}